
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "sqlite3.h"
#ifndef SOFTWARE_NAME
#define SOFTWARE_NAME "example"
#endif
#ifndef VERSION
#define VERSION "NO VERSION"
#endif
#define DB_NAME 	"dbchange/ts-3.1.2.db"
#define TABLE_NAME "'robotDogTask.interface'"

static int sql_callback(void *ptr, int n_column,
    char **column_value, char **column_name)
{
  int i = 0, id = 0, rc;
  double latitude, longitude;//, altitude;
  char tc[4] = {0}, *token = NULL, statement[1024], *errmsg;
  sqlite3 *db = ptr;

  if (n_column == 0) {
    printf("sql: column is 0\n");
    return true;
  }
  for(i = 0; i < n_column; i++) {
    if (!column_value[i])
      continue;
    if (strlen(column_value[i]) > 0) {
      token = column_value[i];
      if (i == 0) {
        id = atoi(token);
      } else if (i == 1) {
        snprintf(tc, 4, "%s", token);
        longitude = atof(tc) + atof(token+3)/60.0;
      } else if (i == 2) {
        snprintf(tc, 3, "%s", token);
        latitude = atof(tc) + atof(token+2)/60.0;
      }
    }
  }
  if ((int)(longitude) == 105) {
    printf("%d %d\n", id, (int)(longitude));
    snprintf(statement, sizeof(statement),
        "update %s set longitude='%f',latitude='%f' where id=%d;",
        TABLE_NAME, longitude, latitude, id);
    rc = sqlite3_exec((sqlite3 *)db, statement, NULL, NULL, &errmsg);
    if (rc != SQLITE_OK) {
      fprintf(stderr, "Error: L%d f-%s %s:%s:%d\n", __LINE__, __func__,
          statement, errmsg, rc);
    }
  }
  return 0;
}
int main()
{
  int rc;
  char *filename = DB_NAME, *errmsg = NULL,
       *statement = "select id,longitude,latitude from 'robotDogTask.interface';";

  sqlite3 *db = NULL;

  rc = sqlite3_open(filename, &db);
  rc = sqlite3_exec((sqlite3 *)db, statement, sql_callback,
      db, &errmsg);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "Error: L%d f-%s %s:%s:%d\n", __LINE__, __func__,
        statement, errmsg, rc);
  }
  sqlite3_close(db);
  return 0;
}

